<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class AddressController extends Controller
{
    //收货地址主页
   	public function address()
   	{
   		$data = DB::table('rec_add')
   			-> leftjoin('region','rec_add.con_region','=','region.region_id')
            -> where('mem_id','=',session('member_id'))
   			->get();
     //     dd(session('member_id'));
   		// dd($data);
   		return view('/home/myhouse/message/address',['data'=>$data]);
   	}

   	//添加收货地址
   	public function addressadd()
   	{
   		$region = DB::table('region') -> select('region_name','region_id') -> get();
   		// dd($region);

   		return view('/home/myhouse/message/addressadd',['region'=>$region]);
   	}

   	//添加到数据库
   	public function insert(Request $request)
   	{
   		$data = $request -> except('_token');
   	  
   		// dd($data);
   		//判断不能为空
   		$this -> validate($request,[
   			'con_region' => 'required',
   			'con_address' => 'required',
   			'con_phone' => 'required',
   			'consignee' => 'required',
   			'con_address' => 'required',
   			'con_phone'=>'regex:/^1[34578][0-9]{9}$/',
   			'con_phone' => 'numeric',
   			],[
   			'con_region.required' => '请选择省份',
   			'con_address.required' => '请填写详细地址',
   			'con_phone.required' => '请填写联系电话',
   			'consignee.required' => '请填写收货人',
   			'con_phone.numeric' => '请输入正确的联系方式',

   			]);
   		
   		
         if(Session::has('member_id'))
         {
            $data['mem_id'] = session('member_id');
            $res = DB::table('rec_add') -> insert($data);
         }else{

            return redirect('/login');
         }
   		if($res)
   		{
   			return redirect('/myhouse/address') -> with(['success'=>'添加成功']);
   		}else
   		{
   			return back() -> with(['error'=>'添加失败']);
   		}
   		// dd($data);
   	}

   	//edit
   	public function edit($id)
   	{
   		$region = DB::table('region') -> select('region_name','region_id') -> get();
   		$data = DB::table('rec_add') 
   			->leftjoin('region','rec_add.con_region','=','region.region_id')
   			-> where('rec_add_id',$id) -> first();
   		// dd($data);
   		return view('/home/myhouse/message/addressedit',['data'=>$data,'region'=>$region]);
   	}

   	//更新收货地址
   	public function update(Request $request)
   	{
   		// return 1111;
   		$data = $request -> except('_token');
   		//判断不能为空
   		$this -> validate($request,[
   			'con_region' => 'required',
   			'con_address' => 'required',
   			'con_phone' => 'required',
   			'consignee' => 'required',
   			'con_address' => 'required',
   			'con_phone'=>'regex:/^1[34578][0-9]{9}$/',
   			'con_phone' => 'numeric',
   			],[
   			'con_region.required' => '请选择省份',
   			'con_address.required' => '请填写详细地址',
   			'con_phone.required' => '请填写联系电话',
   			'consignee.required' => '请填写收货人',
   			'con_phone.numeric' => '请输入正确的联系方式',

   			]);

   		$data['mem_id'] = 1;

   		$res = DB::table('rec_add') -> where('rec_add_id',$data['rec_add_id']) -> update($data);
   		if($res)
   		{
   			return redirect('/myhouse/address') -> with(['success'=>'更新成功']);
   		}else{
   			return back() -> with(['error'=>'更新失败']);
   		}
   		// dd($data);
   	}

   	//删除收货地址
   	public function delete($id)
   	{
   		// dd($id);
   		$res = DB::table('rec_add') -> where('rec_add_id',$id) -> delete();
   		if($res)
   		{
   			return redirect('/myhouse/address') -> with(['success'=>'删除成功']);
   		}else{
   			return back() -> with(['error'=>'删除失败']);
   		}
   	}
}
